package com.example.partner.activity.team;

import static com.example.partner.common.ConfigConstant.BASE_URL;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

;
import com.example.partner.common.BaseResponse;
import com.example.partner.common.SharedViewModel;
import com.example.partner.databinding.FragmentTeamBinding;
import com.example.partner.model.vo.TeamUserVo;
import com.example.partner.adapter.team.TeamAdapter;
import com.example.partner.service.TeamService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class TeamFragment extends Fragment {
    private static final String TAG = "TeamActivity";
    private SharedViewModel sharedViewModel;

    TeamAdapter teamAdapter;
    TeamService teamService;
    private FragmentTeamBinding binding;
    List<TeamUserVo> teamUserVoList;

    FrameLayout build;//点击创建队伍


    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        TeamViewModel teamViewModel =new ViewModelProvider(this).get(TeamViewModel.class);
        binding = FragmentTeamBinding.inflate(inflater, container, false);
        View root = binding.getRoot();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        teamService = retrofit.create(TeamService.class);
        listTeamsbyname("");
        build=binding.buildteam;
        build.setOnClickListener(view -> {
            Intent intent = new Intent(getContext(), BuildTeamActivity.class);
            startActivity(intent);
        });
        binding.btnPublic.setOnClickListener(view -> listTeamsbystatus(0));
        binding.btnLock.setOnClickListener(view -> listTeamsbystatus(2));
        return root;

    }

    private void listTeamsbyname(String teamname) {

        teamService.listTeamsBySearchName(teamname).enqueue(new Callback<BaseResponse<List<TeamUserVo>>>() {
            @Override
            public void onResponse(Call<BaseResponse<List<TeamUserVo>>> call, Response<BaseResponse<List<TeamUserVo>>> response) {
                if (response.isSuccessful()&& response.body() != null) {
                    teamUserVoList = response.body().getData();
                    teamAdapter = new TeamAdapter(getContext(), teamUserVoList);
                    binding.TeamListView.setAdapter(teamAdapter); // 设置新的适配器
                    teamAdapter.setOnItemMoreInfoClickListener(new TeamAdapter.onItemMoreInfoListener() {
                            @Override
                            public void onMoreInfoClick(int i) {//重写接口里的方法
                                // 获取点击的项
                                TeamUserVo selectedTeam = (TeamUserVo) teamAdapter.getItem(i);
                                // 创建Intent并启动新的Activity
                                Intent intent = new Intent(getContext(), TeamInfoActivity.class);
                                intent.putExtra("descript",selectedTeam.getDescription());
                                intent.putExtra("img",selectedTeam.getAvatarUrl());
                                intent.putExtra("teamname", selectedTeam.getName());
                                intent.putExtra("teamid",selectedTeam.getId());
                                intent.putExtra("announce",selectedTeam.getAnnounce());
                                intent.putExtra("status",selectedTeam.getStatus());
                                startActivity(intent);
                            }
                    });
                } else {
                    Log.e(TAG, "请求错误: " + response.message());
                }
            }
            @Override
            public void onFailure(Call<BaseResponse<List<TeamUserVo>>> call, Throwable t) {
                Log.e(TAG, "网络错误: " + t.getMessage());
            }
        });
    }
    private void listTeamsbystatus(Integer status) {

        teamService.listTeamsBySearchStatus(status).enqueue(new Callback<BaseResponse<List<TeamUserVo>>>() {
            @Override
            public void onResponse(Call<BaseResponse<List<TeamUserVo>>> call, Response<BaseResponse<List<TeamUserVo>>> response) {
                if (response.isSuccessful()&& response.body() != null) {
                    teamUserVoList = response.body().getData();
                    teamAdapter = new TeamAdapter(getContext(), teamUserVoList);
                    binding.TeamListView.setAdapter(teamAdapter); // 设置新的适配器
                    teamAdapter.setOnItemMoreInfoClickListener(new TeamAdapter.onItemMoreInfoListener() {
                            @Override
                            public void onMoreInfoClick(int i) {//重写接口里的方法
                                // 获取点击的项
                                TeamUserVo selectedTeam = (TeamUserVo) teamAdapter.getItem(i);
                                // 创建Intent并启动新的Activity
                                Intent intent = new Intent(getContext(), TeamInfoActivity.class);
                                intent.putExtra("teamname", selectedTeam.getName());
                                intent.putExtra("descript",selectedTeam.getDescription());
                                intent.putExtra("img",selectedTeam.getAvatarUrl());
                                intent.putExtra("announce",selectedTeam.getAnnounce());
                                intent.putExtra("teamid",selectedTeam.getId());
                                intent.putExtra("status",selectedTeam.getStatus());
                                startActivity(intent);
                            }
                    });
                } else {
                    Log.e(TAG, "请求错误: " + response.message());
                }
            }
            @Override
            public void onFailure(Call<BaseResponse<List<TeamUserVo>>> call, Throwable t) {
                Log.e(TAG, "网络错误: " + t.getMessage());
            }
        });
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (getActivity() != null) {
            sharedViewModel = new ViewModelProvider(getActivity()).get(SharedViewModel.class);
            sharedViewModel.getSearchQuery().observe(getViewLifecycleOwner(), new Observer<String>() {
                @Override
                public void onChanged(@Nullable String query) {
                    // 当用户提交搜索时，此处会被调用，您可以在这里执行搜索逻辑
                    listTeamsbyname(query);
                }
            });
        }
    }
}


